package com.shycloud.mido.member.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.entity.OrgTeacherDetailed;
import com.shycloud.mido.member.api.entity.SmMemberGroupStudent;
import com.shycloud.mido.member.api.entity.SmMemberTeacherGroup;
import com.shycloud.mido.member.mapper.OrgTeacherDetailedMapper;
import com.shycloud.mido.member.mapper.SmMemberGroupStudentMapper;
import com.shycloud.mido.member.mapper.SmMemberTeacherGroupMapper;
import com.shycloud.mido.member.service.SmMemberTeacherGroupService;

import lombok.AllArgsConstructor;

@Service
@AllArgsConstructor
public class SmMemberTeacherGroupServiceImpl extends ServiceImpl<SmMemberTeacherGroupMapper, SmMemberTeacherGroup> implements SmMemberTeacherGroupService {


	/** 学生分组 mapper */
	private SmMemberTeacherGroupMapper groupMapper;
	/** 教师模块 mapper */
	private OrgTeacherDetailedMapper orgTeacherDetailedMapper;
	/** 分组下学生 mapper */
	private SmMemberGroupStudentMapper groupStudentMapper;

	/**
	 * 教师 新建/编辑 学生分组
	 *
	 * @param name
	 * @param orgId
	 * @return boolean
	 * @author nianhua.jiang
	 * @date 2020/6/22 11:44
	 */
	@Override
	public R saveGroup(String name, Integer orgId, Integer id) {

		//分组为空时报错
		if (StringUtils.isBlank(name)) {
			return new R(CommonConstants.FAIL, "分组名称不能为空", false);
		}

		//根据教师所在机构和memberId获取教师ID
		OrgTeacherDetailed orgTeacherDetailed = orgTeacherDetailedMapper.selectOne(new QueryWrapper<OrgTeacherDetailed>()
				.eq(OrgTeacherDetailed.COL_ORG_ID, orgId)
				.and(x -> x.eq(OrgTeacherDetailed.COL_MEMBER_ID, SecurityUtils.getMember().getId())));

		String teacherId = orgTeacherDetailed.getId();

		//判断教师分组名是否重复
		List<SmMemberTeacherGroup> groupList = groupMapper.selectList(new QueryWrapper<SmMemberTeacherGroup>()
				.eq(SmMemberTeacherGroup.COL_TEACHER_ID, teacherId));

		for (SmMemberTeacherGroup group : groupList) {
			if (group.getName().equals(name)) {
				return new R(CommonConstants.FAIL, "分组名称重复，请重新输入", false);
			}
		}

		//初始化学生分组
		SmMemberTeacherGroup group = new SmMemberTeacherGroup();
		//创建时间
		group.setCreateTime(LocalDateTime.now());
		//分组名称
		group.setName(name);
		//教师ID
		group.setTeacherId(teacherId);
		//ID 更新用【预留】
		group.setId(id);

		return new R(saveOrUpdate(group));

	}

	/**
	 * 移除分组
	 *
	 * @param groupIdList
	 * @return java.lang.Boolean
	 * @author nianhua.jiang
	 * @date 2020/7/30 10:24
	 */
	@Override
	@Transactional(rollbackFor = Exception.class, timeout = 36000)
	public Boolean removeGroup(List<Integer> groupIdList) {

		for (Integer groupId : groupIdList) {

			//1.删除分组
			groupMapper.deleteById(groupId);
			//2.删除分组下的学生
			groupStudentMapper.delete(new QueryWrapper<SmMemberGroupStudent>().eq(SmMemberGroupStudent.COL_GROUP_ID, groupId));

		}

		return true;
	}
}
